package development.socket;

import state.socket.DataPackage;
import development.Global;
import development.bussiness.ChatBussiness;

public class ChatHandle extends RequestGroupHandle {
  public static boolean isOnSignOut = false;
  private ChatBussiness chatBussiness;

  public ChatHandle() {
    chatBussiness = new ChatBussiness();
  }

  public void processPackage(DataPackage dataPackage) {
    super.processPackage(dataPackage);
    int header = dataPackage.getHeader();
    switch (header) {

    /** Authenticate */
    case ProtocolConstants.ResponseHeader.SALT_REPONSE:
      String salt = dataPackage.nextString();
      SocketClientUtil.authenToServerBySessionId(Global.chatClient, Global.loginInfo.getUserName(), Global.session.getId(), salt);
      break;
    case ProtocolConstants.ResponseHeader.AUTHENTICATE_SUCCESS_RESPONSE:
      onAuthenSuccess();
      break;
    case ProtocolConstants.ResponseHeader.AUTHENTICATE_FAIL_RESPONSE:
      break;
      
    /** Chat function */
    case ProtocolConstants.ResponseHeader.CHAT_CREATE_ROOM_SUCCESS_RESPONSE:
      chatBussiness.onCreateRoomSuccess(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHAT_CREATE_ROOM_FAIL_RESPONSE:
      chatBussiness.onCreateRoomFail(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHAT_JOIN_CHAT_ROOM_SUCCESS_RESPONSE:
      chatBussiness.onJoinChatRoomSuccess(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHAT_JOIN_CHAT_ROOM_FAIL_RESPONSE:
      chatBussiness.onJoinChatRoomFail(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHAT_MESSAGE_RECEIVE_RESPONSE:
      chatBussiness.onMessageReceive(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHAT_CHANGE_ROOM_PASSWORD_SUCCESS_RESPONSE:
      chatBussiness.onChangeRoomPasswordSuccess(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHAT_CHANGE_ROOM_PASSWORD_FAIL_RESPONSE:
      chatBussiness.onChangeRoomPasswordFail(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHAT_KICK_MEMBER_SUCCESS_RESPONSE:
      chatBussiness.onKickMemberSuccess(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHAT_KICK_MEMBER_FAIL_RESPONSE:
      chatBussiness.onKickMemberFail(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHAT_BLOCK_USER_SUCCESS_RESPONSE:
      chatBussiness.onBlockUserSuccess(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHAT_BLOCK_USER_FAIL_RESPONSE:
      chatBussiness.onBlockUserFail(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHAT_USER_GET_IN_NOTIFY_RESPONSE:
      chatBussiness.onUserGetInNotify(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHAT_USER_GET_OUT_NOTIFY_RESPONSE:
      chatBussiness.onUserGetOutNotify(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHAT_ROOM_LIST_RESPONSE:
      chatBussiness.onReceiveChatRoomList(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHAT_LEAVE_ROOM_SUCCESS_RESPONSE:
      chatBussiness.onLeaveRoomSuccess(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHAT_LEAVE_ROOM_FAIL_RESPONSE:
      chatBussiness.onLeaveRoomFail(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHAT_ROOM_MEMBER_LIST_RESPONSE:
      chatBussiness.onRoomMemberListReceive(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHAT_GET_ROOM_MEMBER_LIST_FAIL_RESPONSE:
      chatBussiness.onGetRoomMemberListFail(dataPackage);
      break;
    default:
      System.err.println("ChatHandle : Receive unknown message header " + header);
      break;
    }
  }

  public void onConnectDone() {
    super.onConnectDone();
    SocketClientUtil.startAuthenToServer(Global.chatClient, Global.loginInfo.getUserName());
  }

  public void onConnectFail() {
    super.onConnectFail();
  }

  public void onDisconnect() {
    super.onDisconnect();
    if (Global.session != null) {
      SocketClientUtil.connectToChatServer(Global.chatService.getUrl(), Global.chatService.getPort(), Global.chatHandle);
    }
  }

  public void onAuthenSuccess() {
  }
}
